﻿using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;

namespace Microsoft.Cloud.API.Azure.Common
{
    public class FileResult : IHttpActionResult
    {
        private string fileName;
        private byte[] fileContent;

        public FileResult(string fileName, byte[] fileContent)
        {
            this.fileName = fileName;
            this.fileContent = fileContent;
        }

        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            var response = new HttpResponseMessage();
            var stream = new MemoryStream(fileContent);
            response.Content = new StreamContent(stream);
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            response.Content.Headers.ContentDisposition.FileName = fileName;

            return Task.FromResult(response);
        }
    }
}